// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   IVL_TSImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.GTS;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.IVL_TS;
import com.sunwayhorizo.health.hl7.datatypes.PQ;
import com.sunwayhorizo.health.hl7.datatypes.SET_TS;
import com.sunwayhorizo.health.hl7.datatypes.TS;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;
import com.sunwayhorizo.health.hl7.datatypes.common.gtsparser.TimeParser;

import java.io.StringReader;

import java.util.Set;

// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            GTSBase, ANYImpl, BLImpl, CSImpl, 
//            GTSDifferenceImpl, GTSNullImpl, NullFlavorImpl, PQImpl, 
//            REALImpl, STImpl, TSImpl, TYPEImpl, 
//            WidthIVL_TSImpl

public class IVL_TSImpl extends GTSBase implements IVL_TS
{

    public static final String RCS_ID = "$Header: IVL_TSImpl.java 115.56.2050.5 2006/02/22 05:18:50 mmoores noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion(
        "$Header: IVL_TSImpl.java 115.56.2050.5 2006/02/22 05:18:50 mmoores noship $", "oracle.apps.ctb.hl7.types.common");
    protected static final IVL_TS EMPTY_IVL;
    protected TS _low;
    protected TS _high;
    protected BL _lowClosed;
    protected BL _highClosed;
    protected TS _center;
    protected PQ _width;
    private static final IVL_TS nullNI = new IVL_TSImpl(CSImpl.valueOf("NI"));
    private static final IVL_TS nullOTH = new IVL_TSImpl(CSImpl.valueOf("OTH"));
    private static final IVL_TS nullNINF = new IVL_TSImpl(CSImpl.valueOf("NINF"));
    private static final IVL_TS nullPINF = new IVL_TSImpl(CSImpl.valueOf("PINF"));
    private static final IVL_TS nullUNK = new IVL_TSImpl(CSImpl.valueOf("UNK"));
    private static final IVL_TS nullASKU = new IVL_TSImpl(CSImpl.valueOf("ASKU"));
    private static final IVL_TS nullNAV = new IVL_TSImpl(CSImpl.valueOf("NAV"));
    private static final IVL_TS nullMSK = new IVL_TSImpl(CSImpl.valueOf("MSK"));
    private static final IVL_TS nullNA = new IVL_TSImpl(CSImpl.valueOf("NA"));
    private static final IVL_TS nullNASK = new IVL_TSImpl(CSImpl.valueOf("NASK"));


    public static IVL_TS valueOf(String s) throws Hl7Exception
    {
        StringReader stringreader = new StringReader(s);
        TimeParser timeparser = new TimeParser(stringreader);
        try
        {
            IVL_TS ivl_ts = timeparser.ivl_ts();
            GTSBase.checkParserState(timeparser);
            ((GTSBase) ivl_ts)._dataType = com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.IVL_TS;
            return ivl_ts;
        }
        catch (Throwable throwable)
        {
            throw ANYImpl.newParseException("IVL<TS>", s, throwable);
        }
    }


    public static IVL_TS valueOf(TS ts)
    {
        if (ts.isNull().isTrue())
            return nullIVL_TS(NullFlavorImpl.NI);
        PQ pq;
        switch ((int) ts.precision().longValue())
        {
            case 4: // '\004'
                pq = PQImpl.valueOf("1", "a");
                break;

            case 6: // '\006'
                pq = PQImpl.valueOf("1", "mo");
                break;

            case 8: // '\b'
                pq = PQImpl.valueOf("1", "d");
                break;

            case 10: // '\n'
                pq = PQImpl.valueOf("1", "h");
                break;

            case 12: // '\f'
                pq = PQImpl.valueOf("1", "min");
                break;

            case 14: // '\016'
                pq = PQImpl.valueOf("1", "s");
                break;

            default:
                if (ts.precision().longValue() >= 15L)
                    pq = PQImpl.valueOf("1", "ms");
                else
                    throw ANYImpl.newTSPrecisionException("TS", Long.toString(ts.precision().longValue()));
                break;
        }
        TS ts1 = (TS) ts.plus(pq);
        return valueOf(ts, ((BL) (BLImpl.TRUE)), ts1, ((BL) (BLImpl.FALSE)));
    }


    public static IVL_TS valueOf(TS ts, BL bl, TS ts1, BL bl1)
    {
        if (bl == null || ts == null || ts1 == null || bl1 == null)
        {
            if (bl == null)
                throw ANYImpl.newNullException("IVL<TS>", "constructor", "lowClosed");
            if (ts == null)
                throw ANYImpl.newNullException("IVL<TS>", "constructor", "low");
            if (ts1 == null)
                throw ANYImpl.newNullException("IVL<TS>", "constructor", "high");
            if (bl1 == null)
                throw ANYImpl.newNullException("IVL<TS>", "constructor", "highClosed");
        }
        return new IVL_TSImpl(ts, bl, ts1, bl1);
    }


    public static IVL_TS valueOf(TS ts, BL bl, PQ pq, BL bl1)
    {
        return new IVL_TSImpl(ts, bl, pq, bl1);
    }


    public static IVL_TS valueOf(TS ts, BL bl, TS ts1, BL bl1, PQ pq)
    {
        if (pq.isNull().isTrue())
            return new IVL_TSImpl(ts, bl, ts1, bl1);
        if (ts.isNull().isTrue())
            if (ts1.isNull().isTrue())
                return WidthIVL_TSImpl.valueOf(bl, pq, bl1);
            else
                return new IVL_TSImpl(bl, pq, bl1, ts1);
        if (ts1.isNull().isTrue())
            return new IVL_TSImpl(ts, bl, pq, bl1);
        else
            return new IVL_TSImpl(ts, bl, ts1, bl1);
    }


    public static IVL_TS valueOf(TS ts, String s)
    {
        if (ts == null || ts.isNull().isTrue())
            throw ANYImpl.newNullException("IVL<TS>", "constructor", "lowHull");
        if (s == null)
            throw ANYImpl.newNullException("IVL<TS>", "constructor", "highHull");
        int i = ts.literal().length().intValue() - s.length();
        if (i < 0)
            throw ANYImpl.newHullIVLException(ts.literal().stringValue(), s);
        String s1 = ts.literal().stringValue().substring(0, i) + s;
        TS ts1;
        try
        {
            ts1 = TSImpl.valueOf(s1);
        }
        catch (Hl7Exception ctbexception)
        {
            RuntimeException runtimeexception = ANYImpl.newDateException(s1);
            StringBuffer stringbuffer = new StringBuffer(runtimeexception.getMessage());
            stringbuffer.append("\nOriginating Stacktrace:\n");
            stringbuffer.append(ctbexception.toString());
            throw runtimeexception;
        }
        if (ts1.lessThan(ts).isTrue())
        {
            throw ANYImpl.newHullIVLException(ts.literal().stringValue(), s);
        }
        else
        {
            IVL_TS ivl_ts = valueOf(ts);
            IVL_TS ivl_ts1 = valueOf(ts1);
            return ivl_ts.hull(ivl_ts1);
        }
    }


    public static IVL_TS nullIVL_TS()
    {
        return new IVL_TSImpl(NullFlavorImpl.NI);
    }


    protected IVL_TSImpl(TS ts, BL bl, TS ts1, BL bl1)
    {
        _center = TSImpl.nullTS(NullFlavorImpl.NI);
        _width = PQImpl.nullPQ(NullFlavorImpl.PINF);
        _lowClosed = bl;
        _low = ts;
        _high = ts1;
        _highClosed = bl1;
        _literal = STImpl.valueOf(generateLiteral());
    }


    protected IVL_TSImpl(TS low, TS high)
    {

        _low = low;
        _high = high;
    }


    public static IVL_TS valueOf(TS low, TS high)
    {
        return new IVL_TSImpl(low, high);
    }


    protected String generateLiteral()
    {
        StringBuffer stringbuffer = new StringBuffer();
        if (_low.isNull().isFalse())
        {
            String s = _lowClosed.isFalse() ? "]" : "[";
            stringbuffer.append(s);
            stringbuffer.append(_low.literal().stringValue());
        }
        else
        {
            stringbuffer.append("]");
        }
        stringbuffer.append(";");
        if (_high.isNull().isFalse())
        {
            stringbuffer.append(_high.literal().stringValue());
            String s1 = _highClosed.isFalse() ? "[" : "]";
            stringbuffer.append(s1);
        }
        else
        {
            stringbuffer.append("[");
        }
        return stringbuffer.toString();
    }


    protected IVL_TSImpl(TS ts, BL bl, PQ pq, BL bl1)
    {
        _center = TSImpl.nullTS(NullFlavorImpl.NI);
        _width = PQImpl.nullPQ(NullFlavorImpl.PINF);
        _lowClosed = bl;
        _low = ts;
        _high = (TS) ts.plus(pq);
        _width = pq;
        _highClosed = bl1;
        _literal = STImpl.valueOf(generateLiteral());
    }


    protected IVL_TSImpl(BL bl, PQ pq, BL bl1, TS ts)
    {
        _center = TSImpl.nullTS(NullFlavorImpl.NI);
        _width = PQImpl.nullPQ(NullFlavorImpl.PINF);
        _lowClosed = bl;
        _low = (TS) ts.minus(pq);
        _high = ts;
        _width = pq;
        _highClosed = bl1;
        _literal = STImpl.valueOf(generateLiteral());
    }


    protected IVL_TSImpl(BL bl, BL bl1)
    {
        _center = TSImpl.nullTS(NullFlavorImpl.NI);
        _width = PQImpl.nullPQ(NullFlavorImpl.PINF);
        _lowClosed = bl;
        _highClosed = bl1;
        _low = TSImpl.nullTS(NullFlavorImpl.NA);
        _high = TSImpl.nullTS(NullFlavorImpl.NA);
        _center = TSImpl.nullTS(NullFlavorImpl.NA);
    }


    protected IVL_TSImpl(CS cs)
    {
        super(cs);
        _center = TSImpl.nullTS(NullFlavorImpl.NI);
        _width = PQImpl.nullPQ(NullFlavorImpl.PINF);
        _low = TSImpl.nullTS(cs);
        _high = TSImpl.nullTS(cs);
        _lowClosed = BLImpl.nullBL(cs);
        _highClosed = BLImpl.nullBL(cs);
        _width = PQImpl.nullPQ(cs);
        _literal = STImpl.nullST(cs);
    }


    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.IVL_TS;
    }


    public TS low()
    {
        return _low;
    }


    public TS high()
    {
        return _high;
    }


    public BL lowClosed()
    {
        return _lowClosed;
    }


    public BL highClosed()
    {
        return _highClosed;
    }


    public PQ width()
    {
        if (_width.isNull().isTrue())
            if (low().isNull().or(high().isNull()).isTrue())
                _width = PQImpl.nullPQ(NullFlavorImpl.PINF);
            else
                _width = (PQ) _high.minus(_low);
        return _width;
    }


    public TS center()
    {
        if (low().isNull().or(high().isNull()).isTrue())
        {
            return TSImpl.nullTS(NullFlavorImpl.NA);
        }
        else
        {
            PQ pq = width();
            com.sunwayhorizo.health.hl7.datatypes.REAL real = REALImpl.valueOf("0.5");
            PQ pq1 = pq.times(real);
            _center = (TS) low().plus(pq1);
            return _center;
        }
    }


    public BL contains(TS ts)
    {
        return low().lessOrEqual(ts).or(low().isNull()).and(ts.lessOrEqual(high()).or(high().isNull())).and(low().equal(ts).implies(lowClosed()))
            .and(high().equal(ts).implies(highClosed()));
    }


    public BL isEmpty()
    {
        return width().isZero().and(lowClosed().and(highClosed()).not());
    }


    public BL nonEmpty()
    {
        return isEmpty().not();
    }


    public BL notEmpty()
    {
        return isEmpty().not();
    }


    public BL contains(SET_TS set_ts)
    {
        if (set_ts instanceof IVL_TS)
        {
            IVL_TS ivl_ts = (IVL_TS) set_ts;
            return contains(ivl_ts.low()).and(contains(ivl_ts.high()));
        }
        if (set_ts instanceof GTS)
            return contains(((SET_TS) (((GTS) set_ts).hull())));
        else
            return BLImpl.FALSE;
    }


    public INT cardinality()
    {
        throw ANYImpl.newUnsupportedException("IVL<TS>", "cardinality");
    }


    public SET_TS union(SET_TS set_ts)
    {
        if (set_ts instanceof IVL_TS)
        {
            IVL_TS ivl_ts = (IVL_TS) set_ts;
            if (overlaps(ivl_ts).isTrue())
                return union(ivl_ts);
        }
        return super.union(set_ts);
    }


    private IVL_TS union(IVL_TS ivl_ts)
    {
        TS ts = low();
        BL bl = lowClosed();
        if (ts.greaterThan(ivl_ts.low()).or(ivl_ts.low().isNull()).or(ts.equal(ivl_ts.low()).and(bl.not())).isTrue())
        {
            ts = ivl_ts.low();
            if (ts.isNull().isTrue())
                bl = BLImpl.nullBL(NullFlavorImpl.NA);
            else
                bl = ivl_ts.lowClosed();
        }
        TS ts1 = high();
        BL bl1 = highClosed();
        if (ivl_ts.high().greaterThan(ts1).or(ivl_ts.high().isNull()).or(ts1.equal(ivl_ts.high()).and(bl1.not())).isTrue())
        {
            ts1 = ivl_ts.high();
            if (ts1.isNull().isTrue())
                bl1 = BLImpl.nullBL(NullFlavorImpl.NA);
            else
                bl1 = ivl_ts.highClosed();
        }
        if (ts1.lessThan(ts).or(ts1.equal(ts).and(bl1.and(bl).not())).isTrue())
            return nullIVL_TS(NullFlavorImpl.NA);
        else
            return valueOf(ts, bl, ts1, bl1);
    }


    public SET_TS except(SET_TS set_ts)
    {
        if (isNull().or(set_ts.isNull()).isTrue())
            return this;
        if (set_ts instanceof IVL_TS)
        {
            IVL_TS ivl_ts = (IVL_TS) set_ts;
            if (low().isNull().and(high().isNull()).or(ivl_ts.low().isNull().and(ivl_ts.high().isNull())).isTrue())
                return except((GTS) set_ts);
            if (overlaps(ivl_ts).isFalse())
                return this;
            if (ivl_ts.low().lessThan(low()).or(ivl_ts.low().isNull()).or(ivl_ts.low().equal(low()).and(ivl_ts.lowClosed().or(lowClosed().not())))
                .isTrue())
                if (ivl_ts.high().greaterThan(high()).or(ivl_ts.high().equal(high()).and(ivl_ts.highClosed().or(highClosed().not())))
                    .or(ivl_ts.high().isNull()).isTrue())
                    return EMPTY_IVL;
                else
                    return valueOf(ivl_ts.high(), ivl_ts.highClosed().not(), high(), highClosed());
            if (ivl_ts.high().greaterThan(high()).or(ivl_ts.high().equal(high()).and(ivl_ts.highClosed().or(highClosed().not())))
                .or(ivl_ts.high().isNull()).isTrue())
                return valueOf(low(), lowClosed(), ivl_ts.low(), ivl_ts.lowClosed().not());
        }
        return except((GTS) set_ts);
    }


    public GTS except(GTS gts)
    {
        return GTSDifferenceImpl.valueOf(this, gts);
    }


    public IVL_TS intersection(IVL_TS ivl_ts)
    {
        if (isNull().or(ivl_ts.low().isNull().and(ivl_ts.high().isNull())).isTrue())
            return nullIVL_TS(NullFlavorImpl.NA);
        TS ts = low();
        BL bl = lowClosed();
        if (ts.lessThan(ivl_ts.low()).or(ts.isNull()).or(ts.equal(ivl_ts.low()).and(bl)).isTrue())
        {
            ts = ivl_ts.low();
            if (ts.isNull().isTrue())
                bl = BLImpl.nullBL(NullFlavorImpl.NA);
            else
                bl = ivl_ts.lowClosed();
        }
        TS ts1 = high();
        BL bl1 = highClosed();
        if (ivl_ts.high().lessThan(ts1).or(ts1.isNull()).or(ts1.equal(ivl_ts.high()).and(bl1)).isTrue())
        {
            ts1 = ivl_ts.high();
            if (ts1.isNull().isTrue())
                bl1 = BLImpl.nullBL(NullFlavorImpl.NA);
            else
                bl1 = ivl_ts.highClosed();
        }
        if (ts1.lessThan(ts).or(ts1.equal(ts).and(bl1.and(bl).not())).isTrue())
            return nullIVL_TS(NullFlavorImpl.NA);
        else
            return valueOf(ts, bl, ts1, bl1);
    }


    public IVL_TS hull(IVL_TS ivl_ts)
    {
        TS ts = low();
        BL bl = lowClosed();
        if (low().isNull().or(ivl_ts.low().isNull()).isTrue())
        {
            ts = TSImpl.nullTS(NullFlavorImpl.NINF);
            bl = BLImpl.nullBL(NullFlavorImpl.NA);
        }
        else if (ivl_ts.low().lessThan(low()).or(ivl_ts.low().equal(low()).and(bl.not())).isTrue())
        {
            ts = ivl_ts.low();
            bl = ivl_ts.lowClosed();
        }
        TS ts1 = high();
        BL bl1 = highClosed();
        if (high().isNull().or(ivl_ts.high().isNull()).isTrue())
        {
            ts1 = TSImpl.nullTS(NullFlavorImpl.PINF);
            bl1 = BLImpl.nullBL(NullFlavorImpl.NA);
        }
        else if (high().lessThan(ivl_ts.high()).or(high().equal(ivl_ts.high()).and(bl1.not())).isTrue())
        {
            ts1 = ivl_ts.high();
            bl1 = ivl_ts.highClosed();
        }
        if (ts.isNull().and(ts1.isNull()).isTrue())
            return nullIVL_TS(NullFlavorImpl.NA);
        else
            return valueOf(ts, bl, ts1, bl1);
    }


    protected BL overlaps(IVL_TS ivl_ts)
    {
        if (low().isNull().and(high().isNull()).isTrue())
            return BLImpl.FALSE;
        if (ivl_ts.low().isNull().and(ivl_ts.high().isNull()).isTrue())
            return BLImpl.FALSE;
        else
            return contains(ivl_ts.low()).or(contains(ivl_ts.high())).or(ivl_ts.contains(low())).or(ivl_ts.contains(high()));
    }


    public IVL_TS hull()
    {
        return this;
    }


    public IVL_TS nextTo(TS ts)
    {
        if (ts.lessOrEqual(high()).isTrue())
        {
            if (ts.equal(high()).isTrue())
            {
                if (highClosed().isFalse())
                    return this;
                else
                    return nullIVL_TS();
            }
            else
            {
                return this;
            }
        }
        else
        {
            return nullIVL_TS();
        }
    }


    public IVL_TS nextAfter(TS ts)
    {
        IVL_TS ivl_ts = nextTo(ts);
        if (ivl_ts.isNull().isFalse() && ivl_ts.contains(ts).isFalse())
            return this;
        else
            return nullIVL_TS();
    }


    public GTS periodicHull(GTS gts)
    {
        return GTSNullImpl.nullGTS();
    }


    public BL interleaves(GTS gts)
    {
        return BLImpl.FALSE;
    }


    public BL equal(ANY any)
    {
        if (!(any instanceof IVL_TS))
            return BLImpl.FALSE;
        IVL_TS ivl_ts = (IVL_TS) any;
        if (isNull().or(any.isNull()).isTrue())
            return BLImpl.FALSE;
        else
            return lowClosed().equal(ivl_ts.lowClosed()).or(lowClosed().isNull().and(ivl_ts.lowClosed().isNull()))
                .and(highClosed().equal(ivl_ts.highClosed()).or(highClosed().isNull().and(ivl_ts.highClosed().isNull())))
                .and(low().equal(ivl_ts.low()).or(low().isNull().and(ivl_ts.low().isNull())))
                .and(high().equal(ivl_ts.high()).or(high().isNull().and(ivl_ts.high().isNull())));
    }


    public TS demotion()
    {
        if (low().isNull().isFalse())
            if (high().isNull().isFalse())
                return center();
            else
                return low();
        if (high().isNull().isFalse())
            return high();
        else
            return TSImpl.nullTS(NullFlavorImpl.NA);
    }


    public Set toSet()
    {
        throw ANYImpl.newUnsupportedException("IVL<TS>", "toSet");
    }


    // public XMLNode[] writeXML(XMLDocument xmldocument, String s)
    // throws Hl7Exception
    // {
    // try
    // {
    // XMLElement xmlelement = (XMLElement)xmldocument.createElement(s);
    // if(low() != null && low().isNull().isFalse())
    // {
    // XMLNode xmlnode = ((TSImpl)low()).writeXML(xmldocument, "low")[0];
    // if(!lowClosed().isNull().isTrue())
    // ((XMLElement)xmlnode).setAttribute("inclusive",
    // lowClosed().literal().stringValue());
    // xmlelement.appendChild(xmlnode);
    // }
    // if(high() != null && !high().isNull().isTrue())
    // {
    // XMLNode xmlnode1 = ((TSImpl)high()).writeXML(xmldocument, "high")[0];
    // if(!highClosed().isNull().isTrue())
    // ((XMLElement)xmlnode1).setAttribute("inclusive",
    // highClosed().literal().stringValue());
    // xmlelement.appendChild(xmlnode1);
    // }
    // return (new XMLNode[] {
    // xmlelement
    // });
    // }
    // catch(Exception exception)
    // {
    // throw new Hl7Exception(exception);
    // }
    // }

    public static IVL_TS nullIVL_TS(CS cs)
    {
        String s = null;
        if (cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if (s.equals("NI"))
            return nullNI;
        if (s.equals("OTH"))
            return nullOTH;
        if (s.equals("NINF"))
            return nullNINF;
        if (s.equals("PINF"))
            return nullPINF;
        if (s.equals("UNK"))
            return nullUNK;
        if (s.equals("ASKU"))
            return nullASKU;
        if (s.equals("NAV"))
            return nullNAV;
        if (s.equals("MSK"))
            return nullMSK;
        if (s.equals("NA"))
            return nullNA;
        if (s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

    static
    {
        EMPTY_IVL = WidthIVL_TSImpl.valueOf(BLImpl.nullBL(NullFlavorImpl.NI), PQImpl.valueOf(REALImpl.valueOf("0"), CSImpl.valueOf("s")),
            BLImpl.nullBL(NullFlavorImpl.NI));
    }
}
